<?php
class DbMysql {
	public $ver = "1.0";
	private $dbhost; // 数据库主机
	private $dbuser; // 数据库用户名
	private $dbpass; // 数据库用户名密码
	private $dbname; // 数据库名
	private $dou_link; // 数据库连接标识
	private $charset; // 数据库编码，GBK,UTF8,gb2312
	private $pconnect; // 持久链接,1为开启,0为关闭
	private $sql; // sql执行语句
	private $result; // 执行query命令的结果资源标识
	private $error_msg; // 数据库错误提示
	private $table; // 数据库错误提示

	// 构造函数
	function __construct($dbhost="", $dbuser="", $dbpass="", $dbname = '', $charset = 'utf8', $pconnect = 0) {
		$this->dbhost = $dbhost;
		$this->dbuser = $dbuser;
		$this->dbpass = $dbpass;
		$this->dbname = $dbname;
		$this->charset = strtolower(str_replace('-', '', $charset));
		$this->pconnect = $pconnect;
		$this->connect();
	}
	function db($table){
		$this->table = $table;
	}

	function getAll($field = "*",$where = "1=1",$sort = "id desc",$limit = ""){
		$data = array();
		$limit = empty($limit)? $limit : "limit ".$limit;
		$sql="Select ".$field." from `".$this->table."` where ".$where." order by ".$sort." ".$limit;
		$result = $this->query($sql);
		while ($row = $this->fetch_assoc($result)) {
			$data[] = $row;
		}
		return $data;
	}

	function add($data){
		$column = array();
		$datas = array();
		foreach ($data as $key => $one) {
			$column[] = "`".$key."`";
			$datas[] = "'".addslashes($one)."'";
		}
		$column = implode(",", $column);
		$datas = implode(",", $datas);
		$sql="insert into `".$this->table."` (".$column.")values(".$datas.")";
		$result = $this->query($sql);
		if ($result){
			$result = $this->insert_id();
		}
		return $result;
	}

	function getOne($field="*",$where="1=1"){
		$data = array();
		$sql="Select ".$field." from `".$this->table."` where ".$where." limit 1";
		$result = $this->query($sql);
		while ($row = $this->fetch_assoc($result)) {
			$data[] = $row;
		}
		$data[0] = empty($data) ? array() : $data[0];
		return $data[0];
	}

	function edit($data,$where="1=1"){
		$sql = array();
		foreach ($data as $key => $one) {
			$sql[] = "`".$key."` = '".htmlspecialchars($one)."'";
		}
		$sql = implode(",", $sql);
		$sql="update `".$this->table."` set ".$sql." where ".$where;
		$result = $this->query($sql);
		if ($result){
			$result = $this->affected_rows();
		}
		return $result;
	}

	function getData($field,$where="1=1",$empty=""){
		$sql="Select ".$field." from `".$this->table."` where ".$where." limit 1";
		$query = $this->query($sql);
		$result = $this->fetch_array($query);
		if (!empty($empty) && empty($result[$field])){
			return $empty;
		}
		return $result[$field];
	}

	function sql($sql){
		$this->sql = $sql;
		$query = mysql_query($this->sql);
		$result = $this->fetch_assoc($query);
		return $result;
	}

	function del($where="1=1"){
		$sql="delete from `".$this->table."` where ".$where;
		$result = mysql_query($sql);
		if ($result){
			$result = $this->affected_rows();
		}
		return $result;
	}

	function getCount($where="1=1",$table = ""){
		if (empty($table)){
			$count = "count(*)";
		}else{
			$count = "count(distinct ".$table.")";
		}
		$sql="Select ".$count." as num from `".$this->table."` where ".$where;
		$query = $this->query($sql);
		$result = $this->fetch_array($query);
		return $result["num"];
	}


	function getSum($table,$where="1=1"){
        $sql="Select sum(".$table.") as num from `".$this->table."` where ".$where;
        $query = $this->query($sql);
        $result = $this->fetch_array($query);
        $result = empty($result["num"]) ? 0 : $result["num"];
        return $result;
    }

	// 数据库连接
	function connect() {
		if ($this->pconnect) {
			if (!$this->dou_link = @mysql_pconnect($this->dbhost, $this->dbuser, $this->dbpass)) {
				return false;
			}
		} else {
			if (!$this->dou_link = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpass, true)) {
				return false;
			}
		}

		if ($this->version() > '4.1') {
			if ($this->charset) {
				$this->query("SET character_set_connection=" . $this->charset . ", character_set_results=" . $this->charset .", character_set_client=binary");
			}
			if ($this->version() > '5.0.1') {
				$this->query("SET sql_mode=''");
			}
		}

		if (mysql_select_db($this->dbname, $this->dou_link) === false) {
			return false;
		}
	}
	// 数据库执行语句，可执行查询添加修改删除等任何sql语句
	function query($sql) {
		$this->sql = $sql;
		$query = mysql_query($this->sql, $this->dou_link);
		return $query;
	}
	// 取得前一次 MySQL 操作所影响的记录行数
	function affected_rows() {
		return mysql_affected_rows();
	}

	// 返回结果集中一个字段的值
	function result($row = 0) {
		return @ mysql_result($this->result, $row);
	}

	// 返回结果集中行的数目
	function num_rows($query) {
		return @ mysql_num_rows($query);
	}

	// 返回结果集中字段的数
	function num_fields($query) {
		return mysql_num_fields($query);
	}

	// 释放结果内存
	function free_result() {
		return mysql_free_result($this->result);
	}

	// 返回上一步 INSERT 操作产生的 ID
	function insert_id() {
		return mysql_insert_id();
	}

	// 从结果集中取得一行作为数字数组
	function fetch_row($query) {
		return mysql_fetch_row($query);
	}

	// 从结果集中取得一行作为关联数组
	function fetch_assoc($query) {
		return mysql_fetch_assoc($query);
	}

	// 从结果集取得的行生成的数组
	function fetch_array($query) {
		return mysql_fetch_array($query);
	}

	// 返回 MySQL 服务器的信息
	function version() {
		if (empty($this->version)) {
			$this->version = mysql_get_server_info($this->dou_link);
		}
		return $this->version;
	}

	// 关闭 MySQL 连接
	function close() {
		return mysql_close($this->dou_link);
	}

	// 返回错误信息
	function error($msg = '') {
		$msg = $msg ? "Error: $msg" : '<b>MySQL server error report</b><br>' . $this->error_msg;
		exit($msg);
	} 
}
?>